package com.app.service.impl;

import com.app.common.ConstantTool;
import com.app.mapper.jiuquan.ApplyLoanMapper;
import com.app.mapper.jiuquan.LoanInfoMapper;
import com.app.model.ApplyLoanModel;
import com.app.model.LoanInfoModel;
import com.app.service.ApplyLoanService;
import com.app.util.StringTool;
import com.app.vo.ApplyLoanVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplyLoanServiceImpl extends ServiceImpl<ApplyLoanMapper, ApplyLoanModel> implements ApplyLoanService {
    @Autowired
    private ApplyLoanMapper applyLoanMapper;

    @Autowired
    private LoanInfoMapper loanInfoMapper;

    @Override
    public ApplyLoanVo getApplyLoanList(Integer page, Integer limit, String startTime, String endTime, String name,Integer loanId,Integer areaId,String identity) {
        Page<ApplyLoanModel> page1 = new Page<>(page, limit);

        QueryWrapper<ApplyLoanModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(name)){
            qw.eq("name",name);
        }
        if (StringTool.isNotEmpty(identity)){
            qw.eq("identity",identity);
        }
        if (loanId!=null&&loanId>0){
            qw.eq("loan_id",loanId);
        }
        if (areaId!=null&&areaId>0){
            qw.eq("area_id",areaId);
        }
        if (StringTool.isNotEmpty(startTime) && StringTool.isNotEmpty(endTime)) {
            qw.le("create_time", endTime);
            qw.ge("create_time", startTime);
        }

        Page<ApplyLoanModel> applyLoanPage = applyLoanMapper.selectPage(page1, qw);
        List<ApplyLoanModel> applyLoanList = applyLoanPage.getRecords();
        for (ApplyLoanModel applyLoan : applyLoanList) {
            LoanInfoModel loanInfo = loanInfoMapper.selectById(applyLoan.getLoanId());
            applyLoan.setLoanIdText(loanInfo.getTitle());
            applyLoan.setOpenId(null);
            if(applyLoan.getAreaId()!=null)
            {
                for (int i = 0; i < ConstantTool.AREA_VO_LIST.size(); i++) {
                    if(applyLoan.getAreaId().equals(ConstantTool.AREA_VO_LIST.get(i).getId()))
                    {
                        applyLoan.setAreaText(ConstantTool.AREA_VO_LIST.get(i).getData());
                    }
                }
            }

        }

        long count = applyLoanPage.getTotal();

        ApplyLoanVo applyLoanVo = new ApplyLoanVo();
        applyLoanVo.setList(applyLoanList);
        applyLoanVo.setCount((int) count);
        return applyLoanVo;
    }

    @Override
    public ApplyLoanVo getUserApplyLoanList(Integer page, Integer limit, String openId) {
        Page<ApplyLoanModel> page1 = new Page<>(page, limit);
        QueryWrapper<ApplyLoanModel> qw = new QueryWrapper<>();
        qw.eq("open_id", openId);
        Page<ApplyLoanModel> applyLoanPage = applyLoanMapper.selectPage(page1, qw);
        List<ApplyLoanModel> applyLoanList = applyLoanPage.getRecords();
        for (ApplyLoanModel applyLoan : applyLoanList) {
            Integer loanId = applyLoan.getLoanId();
            LoanInfoModel loanInfo = loanInfoMapper.selectById(loanId);
            applyLoan.setLoanIdText(loanInfo.getTitle());
            applyLoan.setOpenId(null);
        }

        long count = applyLoanPage.getTotal();

        ApplyLoanVo applyLoanVo = new ApplyLoanVo();
        applyLoanVo.setList(applyLoanList);
        applyLoanVo.setCount((int) count);
        return applyLoanVo;
    }
}
